/*
 * Primary Interface not used
*/

package com.costeaalex.blueserver.interfaces;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.costeaalex.blueserver.bluecove.BlueCoveServer;

public class ServerInterface
	{
	
	private JFrame mainFrame = new JFrame("BlueServer");
	private JPanel buttonsPanel = new JPanel();
	
	private JButton start = new JButton("Start");
	private StartListener startL = new StartListener();
	
	private JButton accept = new JButton("Accept");
	private AcceptListener acceptL = new AcceptListener();
	
	private JButton send = new JButton("Send");
	private SendListener sendL = new SendListener();
	
	private JButton receive = new JButton("Receive");
	private ReceiveListener receiveL = new ReceiveListener();
	
	private JButton stop = new JButton("Stop");
	private StopListener stopL = new StopListener();
	
	private Container cont;
	private BlueCoveServer server;
	private TextArea textA = new TextArea();
	
	public ServerInterface(BlueCoveServer s)
		{
		server=s;
		}

	public void start()
		{
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		cont=mainFrame.getContentPane();
		
		start.addActionListener(startL);
		accept.addActionListener(acceptL);
		send.addActionListener(sendL);
		receive.addActionListener(receiveL);
		stop.addActionListener(stopL);
		
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
		buttonsPanel.add(start);
		buttonsPanel.add(accept);
		buttonsPanel.add(send);
		buttonsPanel.add(receive);
		buttonsPanel.add(stop);
		cont.add(BorderLayout.WEST, buttonsPanel);
		cont.add(BorderLayout.CENTER,textA);
				
		mainFrame.setSize(400,300);
		mainFrame.setVisible(true);
		}
	
	public class StartListener implements ActionListener
		{
		public void actionPerformed(ActionEvent event)
			{
			server.start();
			textA.append("Server started...\n");
			}
		}
	
	public class AcceptListener implements ActionListener
	{
	public void actionPerformed(ActionEvent event)
		{
		//server.acceptClients();
		textA.append("Accepting clients...\n");
		}
	}
	
	public class SendListener implements ActionListener
		{
		public void actionPerformed(ActionEvent event)
			{
			server.send(97);
			textA.append("Sent\n");
			}
		}
	
	public class ReceiveListener implements ActionListener
		{
		public void actionPerformed(ActionEvent event)
			{
			int data;
			data=server.receive();
			textA.append("Received "+data+"\n");
			}
		}
	
	public class StopListener implements ActionListener
		{
		public void actionPerformed(ActionEvent event)
			{
			server.stop();
			textA.append("Server stopped\n");
			}
		}
	
	}
